package com.tledu.cyrus.web;

import com.alibaba.fastjson2.JSONObject;
import com.tledu.cyrus.core.AjaxResult;
import com.tledu.cyrus.core.BaseHttpServlet;
import com.tledu.cyrus.core.IServletBusinessExecute;
import com.tledu.cyrus.dao.impl.UserDaoImpl;
import com.tledu.cyrus.domain.User;
import com.tledu.cyrus.service.IUserService;
import com.tledu.cyrus.service.impl.UserServiceImpl;
import com.tledu.cyrus.utils.RequestUtils;
import com.tledu.cyrus.utils.ResponseUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author cyrus
 */
@WebServlet("/user/*")
public class LoginServlet extends BaseHttpServlet {

    private IUserService userService;

    @Override
    public void init() throws ServletException {
        userService = new UserServiceImpl(new UserDaoImpl());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo = req.getPathInfo();

        switch (pathInfo) {
            case "/login":
                // 实现登录功能
                JSONObject jsonObject = RequestUtils.getReqJsonBody(req, JSONObject.class);

//                IServletBusinessExecute servletBusinessExecute = new IServletBusinessExecute() {
//                    @Override
//                    public void execute() throws Exception {
//                        User user = userService.login(jsonObject.getString("username"), jsonObject.getString("password"));
//                        // 登录成功
//                        // 设置登录用户
//                        req.getSession().setAttribute("user", user);
//                        ResponseUtils.responseJson(resp, AjaxResult.success(user));
//                    }
//                };
//
//                executeBusiness(resp,servletBusinessExecute);

//                IServletBusinessExecute servletBusinessExecute = () -> {
//                    User user = userService.login(jsonObject.getString("username"), jsonObject.getString("password"));
//                    // 登录成功
//                    // 设置登录用户
//                    req.getSession().setAttribute("user", user);
//                    ResponseUtils.responseJson(resp, AjaxResult.success(user));
//                };
//
//                executeBusiness(resp,servletBusinessExecute);

                executeBusiness(resp, () -> {
                    User user = userService.login(jsonObject.getString("username"), jsonObject.getString("password"));
                    // 登录成功
                    // 设置登录用户
                    req.getSession().setAttribute("user", user);
//                    ResponseUtils.responseJson(resp, AjaxResult.success(user));
                    AjaxResult a = new AjaxResult();
                    a.setCode(200);
                    a.setMsg("请求成功");
                    ResponseUtils.responseJson(resp, a);
                });
                break;
            case "/register":
                // 实现注册的逻辑
                User user = RequestUtils.getReqJsonBody(req, User.class);
                executeBusiness(resp, () -> {
                    ResponseUtils.responseJson(resp, AjaxResult.success(userService.register(user)));
                });
        }

    }


}
